<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
 <head>
  <title>inputEx - Autocomplete Usage</title>

<!-- YUI CSS -->
<link rel="stylesheet" type="text/css" href="../lib/yui/reset/reset-min.css">
<link rel="stylesheet" type="text/css" href="../lib/yui/fonts/fonts-min.css">
<link rel="stylesheet" type="text/css" href="../lib/yui/container/assets/container.css"> 
<link rel="stylesheet" type="text/css" href="../lib/yui/assets/skins/sam/skin.css">


<!-- InputEx CSS -->
<link type='text/css' rel='stylesheet' href='../css/inputEx.css' />

<!-- Demo CSS -->
<link rel="stylesheet" type="text/css" href="css/demo.css"/>
<link rel="stylesheet" type="text/css" href="css/dpSyntaxHighlighter.css">

 </head>

 <body class="yui-skin-sam">

<p class='demoTitle'>inputEx - Autocomplete Usage</p>

<!-- Example 1 -->
<div class='exampleDiv'>

	<p class="title">Basic Autocomplete creation</p>
	
	<p>Use the following code to create a basic inputEx Autocomplete.</p>

	<div class='demoContainer' id='container1'></div>

	<div class='codeContainer'>
		<textarea name="code" class="JScript">
			// Autocompleter
			var field = inputEx({
				type: "autocomplete",
				inputParams: {
					parentEl: 'container1', 
					label: 'Search US state',
					datasource: new YAHOO.widget.DS_JSFunction( getStates), // see http://developer.yahoo.com/yui/examples/autocomplete/assets/js/states_jsfunction.js for getState function

					// Format the hidden value (value returned by the form)
					returnValue: function(oResultItem) {
						return oResultItem[1];
					},
					autoComp: {
						forceSelection: true,
						minQueryLength: 2,
						maxResultsDisplayed: 50,
						formatResult: function(oResultItem, sQuery) {
			       	var sMarkup = oResultItem[0] + " (" + oResultItem[1] + ")";
			       	return sMarkup;
			    	}
					}	
				}
			});

			var button = inputEx.cn('button', null, null, 'GetValue');
			YAHOO.util.Dom.get('container1').appendChild(button);
			YAHOO.util.Event.addListener(button , 'click', function() {
				alert( field.getValue() );
			});

			var logDiv = YAHOO.inputEx.cn('div', null, null, "");
			YAHOO.util.Dom.get('container1').appendChild(logDiv);
			field.updatedEvt.subscribe(function(e,params) {
				var value = params[0];
				logDiv.innerHTML += "Updated at "+(new Date())+" with value '"+value+"'<br />";
			});
		</textarea>
	</div>

</div>



<!-- Example 2 -->
<div class='exampleDiv'>

	<p class="title">Delicious Autocompleter</p>
	
	<p>Uses the <a href="http://del.icio.us/help/json/posts">del.icio.us json API</a> to search within posts</p>

	<div class='demoContainer' id='container2'></div>

	<div class='codeContainer'>
		<textarea name="code" class="JScript">
			// Delicious DataSource using a JSFunction
			// Delicious.posts is set by the http://feeds.delicious.com/feeds/json/neyric?count=100 script included in the page
			var deliciousDS = new YAHOO.widget.DS_JSFunction(function (sQuery) {
		    if (!sQuery || sQuery.length == 0) return false;
				var query = sQuery.toLowerCase();
			  var aResults = [];
				for(var i = 0 ; i < Delicious.posts.length ; i++) {
					var desc = Delicious.posts[i].d.toLowerCase();
					if( desc.indexOf(query) != -1) {
						aResults.push([Delicious.posts[i].d, Delicious.posts[i]]);
					}
				}
				return aResults;
		 	});
			deliciousDS.maxCacheEntries = 100;

			var deliciousAC = inputEx({
				type: "autocomplete",
				inputParams: {
					label: 'Search my delicious bookmarks',
					description: 'Try "javascript"',
					parentEl: 'container2', 
					name: 'chosen_url',
					datasource: deliciousDS,
					// Format the hidden value (value returned by the form)
					returnValue: function(oResultItem) {
						var post = oResultItem[1];
						return post.u;
					},
					// Autocompleter options
					autoComp: {
						forceSelection: true,
						minQueryLength: 2,
						maxResultsDisplayed: 50,
						formatResult: function(oResultItem, sQuery) {
							var post = oResultItem[1];
			       	return '<a href="'+post.u+'">visit</a> <span target="_new">'+post.d+'</span>';
			    	}
					}
				}
			});
			var button = inputEx.cn('button', null, null, 'GetValue');
			YAHOO.util.Dom.get('container2').appendChild(button);
			YAHOO.util.Event.addListener(button , 'click', function() {
				alert( deliciousAC.getValue() );
			});
		</textarea>
	</div>

</div>

<!-- YUI Library -->
<script type="text/javascript" src="../lib/yui/yahoo/yahoo-min.js"></script> 
<script type="text/javascript" src="../lib/yui/dom/dom-min.js"></script> 
<script type="text/javascript" src="../lib/yui/event/event-min.js"></script>
<script type="text/javascript" src="../lib/yui/datasource/datasource-min.js"></script>
<script type="text/javascript" src="../lib/yui/autocomplete/autocomplete-min.js"></script>

<!-- InputEx -->
<script src="../js/inputex.js"  type='text/javascript'></script>
<script src="../js/Field.js"  type='text/javascript'></script>
<script src="../js/Visus.js"  type='text/javascript'></script>
<script src="../js/fields/StringField.js"  type='text/javascript'></script>
<script src="../js/fields/AutoComplete.js"  type='text/javascript'></script>

<!-- For the state autocompleter -->
<script src="http://developer.yahoo.com/yui/examples/autocomplete/assets/js/states_jsfunction.js" type='text/javascript'></script>

<!-- For the delicious autocompleter -->
<script src="http://feeds.delicious.com/feeds/json/neyric?count=100" type='text/javascript'></script>


<script>

YAHOO.util.Event.addListener(window, 'load', function() {

	// Example 1
	
	// Autocompleter
	var field = inputEx({
		type: "autocomplete",
		inputParams: {
			parentEl: 'container1', 
			label: 'Search US state',
			typeInvite:'Enter US state',
			datasource: new YAHOO.widget.DS_JSFunction( getStates), // see http://developer.yahoo.com/yui/examples/autocomplete/assets/js/states_jsfunction.js for getState function
			
			// Format the hidden value (value returned by the form)
			returnValue: function(oResultItem) {
				return oResultItem[1];
			},
			autoComp: {
				forceSelection: true,
				minQueryLength: 2,
				maxResultsDisplayed: 50,
				formatResult: function(oResultItem, sQuery) {
	       	var sMarkup = oResultItem[0] + " (" + oResultItem[1] + ")";
	       	return sMarkup;
	    	}
			}	
		}
	});
	
	var button = inputEx.cn('button', null, null, 'GetValue');
	YAHOO.util.Dom.get('container1').appendChild(button);
	YAHOO.util.Event.addListener(button , 'click', function() {
		alert( field.getValue() );
	});
	
	var logDiv = YAHOO.inputEx.cn('div', null, null, "");
	YAHOO.util.Dom.get('container1').appendChild(logDiv);
	field.updatedEvt.subscribe(function(e,params) {
		var value = params[0];
		logDiv.innerHTML += "Updated at "+(new Date())+" with value '"+value+"'<br />";
	});
	
	
	
	// Example 2: Delicious autocompleter
	
	// Delicious DataSource using a JSFunction
	// Delicious.posts is set by the http://feeds.delicious.com/feeds/json/neyric?count=100 script included in the page
	var deliciousDS = new YAHOO.widget.DS_JSFunction(function (sQuery) {
    if (!sQuery || sQuery.length == 0) return false;
		var query = sQuery.toLowerCase();
	  var aResults = [];
		for(var i = 0 ; i < Delicious.posts.length ; i++) {
			var desc = Delicious.posts[i].d.toLowerCase();
			if( desc.indexOf(query) != -1) {
				aResults.push([Delicious.posts[i].d, Delicious.posts[i]]);
			}
		}
		return aResults;
 	});
	deliciousDS.maxCacheEntries = 100;
	
	var deliciousAC = inputEx({
		type: "autocomplete",
		inputParams: {
			label: 'Search my delicious bookmarks',
			description: 'Try "javascript"',
			parentEl: 'container2', 
			name: 'chosen_url',
			datasource: deliciousDS,
			// Format the hidden value (value returned by the form)
			returnValue: function(oResultItem) {
				var post = oResultItem[1];
				return post.u;
			},
			// Autocompleter options
			autoComp: {
				forceSelection: true,
				minQueryLength: 2,
				maxResultsDisplayed: 50,
				formatResult: function(oResultItem, sQuery) {
					var post = oResultItem[1];
	       	return '<a href="'+post.u+'">visit</a> <span target="_new">'+post.d+'</span>';
	    	}
			}
		}
	});
	var button = inputEx.cn('button', null, null, 'GetValue');
	YAHOO.util.Dom.get('container2').appendChild(button);
	YAHOO.util.Event.addListener(button , 'click', function() {
		alert( deliciousAC.getValue() );
	});
	
});

</script>

<script src="js/dpSyntaxHighlighter.js"></script>
<script language="javascript"> 
dp.SyntaxHighlighter.HighlightAll('code');
</script>

 </body>
</html>